<template>
	<section>
		<!--工具条  高级查询 + 新增-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary"  v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
    <el-table :data="Systemdictionarytypes"  highlight-current-row v-loading="listLoading"
              @selection-change="selsChange"
              style="width: 100%;"   @row-dblclick="editCell">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="字典名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="name" label="字典编号" width="100" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small"  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--双击事件-->
    <el-dialog :title="title" :visible.sync="mxVisible" >
      <el-table :data="all">
        <el-table-column prop="name" label="字典明细" width="100" sortable>
        </el-table-column>
      </el-table>
    </el-dialog>

		<!--工具条  批量删除 + 分页栏-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="totals" :current-page="currentPage" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="字典名称" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典编号" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

	export default {
		data() {
			return {
        mxVisible: false,
        all:[],
			  //1.分页查询的相关参数
        Systemdictionarytypes: [],
			  pageSize:5,
        currentPage:1,
        totals:0,
        listLoading: false,

        //2.高级查询的相关数据
        keyword: '',

        //3.批量操作的相关数据
        sels: [],//列表选中列

				//4.添加或修改的相关数据
        title: '',
        saveFormVisible: false,
        saveLoading: false,
				saveFormRules: {
          name: [
            {required: true, message: '字典名称', trigger: 'blur'}
          ],
          sn: [
            {required: true, message: '字典编号', trigger: 'blur'}
          ]
				},
				//5.添加和修改中对话框的数据
				saveForm: {
          sn:'',
          name: null,

				},

        //6.显示所有的部门经理
        managers:[],

        //7.显示部门树
        deptTree: []
      }
		},

		methods: {
		  //双击事件
      editCell(row){
        console.log(row.id)
       this.title="数据字典明细"
       this.mxVisible=true
       this.$http.get("/Systemdictionarydetail/type/"+row.id).then(res =>{
         this.all=res.data;
       })
      },

      //1.获取部门列表
      getSystemdictionarytypes() {
        let para = {
          pageSize: this.pageSize,//分页查询的
          currentPage:this.currentPage,//分页查询的
          keyword: this.keyword //高级查询的
        };
        this.listLoading = true;
        this.$http.post("/Systemdictionarytype",para).then(res=>{
          this.Systemdictionarytypes = res.data.data;
          this.totals= res.data.totals;
          this.listLoading = false;
        }).catch(res=>{
          this.$message.error("系统繁忙，稍后再试")
        })
      },
      //2.点击分页实现
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getSystemdictionarytypes();
      },

      //3.高级查询 = 关键字查询
      keywordQuery(){
        this.currentPage = 1;
        this.getSystemdictionarytypes();
      },

			//4.删除
			handleDel: function (index, row) {
        console.log(row)
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/Systemdictionarytype/"+row.id).then(res=>{
            this.listLoading = false;
            if(res.data.success){
              this.$message.success("删除成功");
              this.getSystemdictionarytypes();
            }else {
              this.$message.error("系统繁忙，请稍后重试!!![500]");
            }
          }).catch(res=>{
            this.$message.error("系统繁忙，请稍后重试!!![400,404]");
          })
				});
			},

      ////5.选项改变的时候触发的逻辑
      selsChange: function (sels) {
        this.sels = sels;
      },

      //6.批量删除逻辑
      batchRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.patch("/Systemdictionarytype",ids).then(res=>{
            if(res.data.success) { //操作成功
              this.currentPage = 1;
              this.getSystemdictionarytypes();
            }else {
              this.$message.error("系统繁忙，请稍后重试!!![500]");
            }
          }).catch(res=>{
            this.$message.error("系统繁忙，请稍后重试!!![400,404]");
          })
        });
      },

			//7.点击修改按钮弹出对话框
			handleEdit: function (index, row) {
        this.title = "编辑";
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
			},
			//8.点击新增按钮弹出对话框
			handleAdd: function () {
        this.title = "新增";
				this.saveFormVisible = true;
				this.saveForm = {
          sn:'',
          name: null,
				};

			},
			//9.点击保存提交数据到后端
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
              let para = Object.assign({}, this.saveForm);
              this.$http.put("/Systemdictionarytype",para).then(res=>{
                this.saveLoading = false;  //关闭加载框
                this.saveFormVisible = false; //关闭对话框
                if(res.data.success){
                  this.getSystemdictionarytypes();
                }else{
                  this.$message.error("系统繁忙，请稍后重试!!![500]");
                }
              }).catch(res=>{
                this.$message.error("系统繁忙，请稍后重试!!![400,404]");
              })
            });
          }
				});
			},
		},
		mounted() {
			this.getSystemdictionarytypes();
		}
	}

</script>

<style scoped>

</style>